package com.szf.util;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class MapUtil {
	public static <K, V> List<K> getKeyList(Map<K, V> map) {
		List<K> list = new ArrayList<K>();
		for (K key : map.keySet()) {
			list.add(key);
		}
		return list;
	}

	public static <K, V> List<V> getValueList(Map<K, V> map) {
		List<V> list = new ArrayList<V>();
		for (V value : map.values()) {
			list.add(value);
		}
		return list;
	}

	/**
	 * 从map获取字符串
	 */
	public static String getMap(Map map, String str, String dv) {
		if (str == null)
			return dv;
		str = str.trim();
		if (str.length() == 0)
			return dv;
		if (map == null)
			return dv;
		if (map.containsKey(str) == false)
			return dv;
		if (map.containsKey(str) == false)
			return dv;
		String s = "" + map.get(str);
		s = s.trim();
		if (s.length() == 0) {
			return dv;
		}
		return s;
	}

	public static String getMap(Map map, String str) {
		return getMap(map, str, "");
	}

	/**
	 * 从map获取整型
	 */
	public static int getMapInt(Map map, String str, int dv) {
		int value = 0;
		if (str == null)
			return dv;
		str = str.trim();
		if (str.length() == 0)
			return dv;
		if (map == null)
			return dv;
		if (map.containsKey(str) == false)
			return dv;
		String s = "" + map.get(str);
		s = s.trim();
		if (s.length() == 0)
			return dv;
		try {
			value = Integer.parseInt(s);
		} catch (Exception e) {
			return dv;
		}
		return value;
	}

	public static int getMapInt(Map map, String str) {
		return getMapInt(map, str, -1);
	}

	/**
	 * 从map获取整型
	 */
	public static float getMapFloat(Map map, String str, float dv) {
		float value = 0;
		if (str == null)
			return dv;
		str = str.trim();
		if (str.length() == 0)
			return dv;
		if (map == null)
			return dv;
		if (map.containsKey(str) == false)
			return dv;
		String s = "" + map.get(str);
		s = s.trim();
		if (s.length() == 0)
			return dv;
		try {
			value = Float.parseFloat(s);
		} catch (Exception e) {
			return dv;
		}
		return value;
	}

	public static float getMapFloat(Map map, String str) {
		return getMapFloat(map, str, -1);
	}

	public static double getMapDouble(Map map, String str, double dv) {
		double value = 0;
		if (str == null)
			return dv;
		str = str.trim();
		if (str.length() == 0)
			return dv;
		if (map == null)
			return dv;
		if (map.containsKey(str) == false)
			return dv;
		String s = "" + map.get(str);
		s = s.trim();
		if (s.length() == 0)
			return dv;
		try {
			value = Double.parseDouble(s);
		} catch (Exception e) {
			return dv;
		}
		return value;
	}

	public static double getMapDouble(Map map, String str) {
		return getMapDouble(map, str, -1);
	}

	public static Map<String, String> param(Object... kvs) {
		if (kvs == null) {
			return new HashMap<String, String>();
		}
		Map<String, String> param = new HashMap<String, String>();
		int size = kvs.length / 2;
		for (int i = 0; i < size; i++) {
			String key = String.valueOf(kvs[2 * i]);
			String val = String.valueOf(kvs[2 * i + 1]);
			param.put(key, val);
		}
		return param;
	}

}
